
package cool.taomu.toolkit.task.dsl

import com.google.common.base.Optional
import com.google.inject.Inject

import cool.taomu.toolkit.service.client.kademlia.service.ServiceCentreService
import cool.taomu.toolkit.service.kademlia.entity.RequestDto
import cool.taomu.toolkit.service.kademlia.thrift.Response
import cool.taomu.toolkit.service.kademlia.thrift.ServiceEntity

class Task {
	
	static class 服务{
		
		@Inject
		ServiceCentreService scservice
		
		void 获取(String serviceName,@DelegatesTo(RequestDto.class) Closure<RequestDto> closure,
			@DelegatesTo(Response.class) Closure<Response> response = null) {
			def dto = new RequestDto(method:"GET",serviceName:serviceName)
			closure.delegate = dto;
			closure.resolveStrategy = Closure.DELEGATE_FIRST
			closure.call()
			def serviceEntity = new ServiceEntity();
			serviceEntity.body = dto.body
			serviceEntity.headers = dto.headers
			serviceEntity.method = dto.method
			serviceEntity.contentType = dto.contentType
			serviceEntity.params = dto.params
			// serviceEntity.cookie = Arrays.asList(request.cookies);
			serviceEntity.serviceName = serviceName
			def result = scservice.finedService(serviceEntity)
			if(Optional.fromNullable(response).present) {
				response.call(result)	
			}
		}
		
		void 添加(String serviceName,@DelegatesTo(RequestDto.class) Closure<RequestDto> closure) {
			def dto = new RequestDto(method:"POST",serviceName:serviceName)
			closure.delegate = dto;
			closure.resolveStrategy = Closure.DELEGATE_FIRST
			closure.call()
			def serviceEntity = new ServiceEntity();
			serviceEntity.body = dto.body
			serviceEntity.headers = dto.headers
			serviceEntity.method = dto.method
			serviceEntity.contentType = dto.contentType
			serviceEntity.params = dto.params
			// serviceEntity.cookie = Arrays.asList(request.cookies);
			serviceEntity.serviceName = serviceName
			scservice.finedService(serviceEntity)
		}
		
		void 修改(String serviceName,@DelegatesTo(RequestDto.class) Closure<RequestDto> closure) {
			def dto = new RequestDto(method:"PUT",serviceName:serviceName)
			closure.delegate = dto;
			closure.resolveStrategy = Closure.DELEGATE_FIRST
			closure.call()
			def serviceEntity = new ServiceEntity();
			serviceEntity.body = dto.body
			serviceEntity.headers = dto.headers
			serviceEntity.method = dto.method
			serviceEntity.contentType = dto.contentType
			serviceEntity.params = dto.params
			// serviceEntity.cookie = Arrays.asList(request.cookies);
			serviceEntity.serviceName = serviceName
			scservice.finedService(serviceEntity)
		}
		
		void 删除(String serviceName,@DelegatesTo(RequestDto.class) Closure<RequestDto> closure) {
			def dto = new RequestDto(method:"DELETE",serviceName:serviceName)
			closure.delegate = dto;
			closure.resolveStrategy = Closure.DELEGATE_FIRST
			closure.call()
			def serviceEntity = new ServiceEntity();
			serviceEntity.body = dto.body
			serviceEntity.headers = dto.headers
			serviceEntity.method = dto.method
			serviceEntity.contentType = dto.contentType
			serviceEntity.params = dto.params
			// serviceEntity.cookie = Arrays.asList(request.cookies);
			serviceEntity.serviceName = serviceName
			scservice.finedService(serviceEntity)
		}
	}

	void service(@DelegatesTo(服务.class) Closure<服务> closure) {
		def dto = injector.getInstance(服务.class)
		closure.delegate = dto;
		closure.resolveStrategy = Closure.DELEGATE_FIRST
		closure.call()
	}
}